package top.codedance.iotp.iot.sdk;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.common.util.HttpRequestUtil;

public abstract class AbstractService extends AbstractIotService implements IotRegistryService, ModbusSlaveService {

    protected String serverHost;
    protected String deviceId = null;
    protected int requestTimeout = -1;
    protected String noneBoby = new JSONObject().toJSONString();

    public AbstractService(String serverHost) {
        this(serverHost, -1);
    }

    public AbstractService(String serverHost, int requestTimeout) {
        this.serverHost = serverHost;
        this.requestTimeout = requestTimeout;
    }

    public AbstractService(String serverHost, String deviceId) {
        this(serverHost, deviceId, -1);
    }

    public AbstractService(String serverHost, String deviceId, int requestTimeout) {
        this.serverHost = serverHost;
        this.deviceId = deviceId;
        this.requestTimeout = requestTimeout;
    }

    protected JSONObject request(String url, String params){
        if(this.deviceId != null) {
            JSONObject jsonObject = JSONObject.parseObject(params);
            jsonObject.put("deviceId", deviceId);
            params = jsonObject.toJSONString();
        }
        JSONObject post = HttpRequestUtil.post(this.serverHost + "/rest/api" + url, params, requestTimeout);
        return reductionData(post);
    }
}
